package com.medusa.aps.business.modules.modeling.controller;


import com.medusa.aps.business.model.pagination.Page;
import com.medusa.aps.business.modules.modeling.model.input.EquipmentDTO;
import com.medusa.aps.business.modules.modeling.model.input.EquipmentParam;
import com.medusa.aps.business.modules.modeling.mp.entity.Equipment;
import com.medusa.aps.business.modules.modeling.mp.service.EquipmentService;
import com.medusa.aps.business.common.model.resp.Result;
import com.medusa.aps.business.common.web.valid.group.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备控制层
 *
 * @author yuxh
 * @since 2023-08-14 17:37:44
 */
@Validated
@RestController
@RequestMapping("/equipment")
@Tag(name = "设备管理", description = "设备管理")
public class EquipmentController {
    /**
     * 服务对象
     */
    @Resource
    private EquipmentService equipmentService;

    /**
     * 分页查询设备
     *
     * @param equipmentParam 设备查询参数
     * @return 所有数据
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询设备")
    public Result<Page<Equipment>> pageEquipment(EquipmentParam equipmentParam) {
        return Result.ok(equipmentService.pageEquipment(equipmentParam));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键id
     * @return 单条数据
     */
    @GetMapping("/getOne")
    @Operation(summary = "通过主键查询单条数据")
    public Result<Equipment> selectOne(@RequestParam("id") Long id) {
        return Result.ok(equipmentService.getById(id));
    }

    /**
     * 批量新增设备
     *
     * @param equipmentCreates 实体对象
     */
    @PostMapping("/save")
    @Operation(summary = "新增数据")
    public Result<Void> saveBatchEquipment(@RequestBody @Valid @Size(min = 1, max = 50, message = "添加设备个数最小为1,最大为50") List<EquipmentDTO> equipmentCreates) {
        equipmentService.saveBatchEquipment(equipmentCreates);
        return Result.ok();
    }

    /**
     * 修改数据
     *
     * @param equipmentDTO 实体对象
     */
    @PutMapping("/update")
    @Operation(summary = "修改数据")
    public Result<Void> updateEquipment(@RequestBody @Validated(Update.class) EquipmentDTO equipmentDTO) {
        equipmentService.updateEquipment(equipmentDTO);
        return Result.ok();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除数据")
    public Result<Void> delete(@RequestBody List<Long> idList) {
        equipmentService.deleteBatch(idList);
        return Result.ok();
    }
}

